package clases;

import java.awt.Color;
import java.awt.Font;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

public class ventanaLogin extends javax.swing.JFrame {

    Connection connection;
    Ventana v;
    ArrayList<String> usuariosAutorizados;

    public ventanaLogin(Connection conector, Ventana principal, ArrayList<String> autorizados) {
        initComponents();
        usuariosAutorizados = autorizados;
        connection = conector;
        v = principal;

    }

    public Connection conectar(String user, String pass) {

        String nomb_bd = "compu_system";//"myescuela";
        String usuario = user;//"root";
        String clave = pass;//"jobroot";
        String dir_bd = "jdbc:mysql://localhost/" + nomb_bd;
        try {
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            connection = DriverManager.getConnection(dir_bd, usuario, clave);
            if (connection != null) {
                v.setConnection(connection);
                System.out.println("Conexion Establecida");
                if (usuario.equals(usuariosAutorizados.get(0))) {
                    System.out.println(""+usuariosAutorizados.get(0));
                    v.admin = true;
                    v.setPanelGerencia();
                    v.pGerente.getInPanels();
                    v.activeMenu();
                    Statement st = connection.createStatement();
                    v.setStatement(nomb_bd, st);
                    v.setSize(900,600);
                    
                } else {
                    v.setPanelEmpleado();
                    v.desactiveMenu();
                    Statement st = connection.createStatement();
                    v.setStatement(nomb_bd, st);
                    v.pEmpleado.setVisible(true);
                    v.setSize(500, 450);
                    v.admin = false;
                }
                //  stmt = connection.createStatement();

                this.setVisible(false);
                v.setVisible(true);
            }
        } catch (SQLException ex) {
            System.out.println("Error en conexion: " + ex);
        } catch (ClassNotFoundException ex) {
            System.out.println("Conector no encontrado: " + ex);
        } catch (InstantiationException ex) {
            System.out.println(ex.getMessage());
        } catch (IllegalAccessException ex) {
            System.out.println(ex.getMessage());

        }
        
        
        usuario="";
        clave = "";
        return connection;

    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jPasswordField1 = new javax.swing.JPasswordField();
        jButton1 = new javax.swing.JButton();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setBackground(new java.awt.Color(102, 255, 102));
        getContentPane().setLayout(null);

        jPanel1.setOpaque(false);

        jLabel1.setForeground(new java.awt.Color(255, 255, 255));
        jLabel1.setText("User");

        jLabel2.setForeground(new java.awt.Color(255, 255, 255));
        jLabel2.setText("Password");

        jButton1.setText("Entrar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jPasswordField1, javax.swing.GroupLayout.DEFAULT_SIZE, 138, Short.MAX_VALUE)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2)
                            .addComponent(jTextField1)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(58, 58, 58)
                        .addComponent(jButton1)))
                .addContainerGap(37, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPasswordField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 30, Short.MAX_VALUE)
                .addComponent(jButton1)
                .addContainerGap())
        );

        getContentPane().add(jPanel1);
        jPanel1.setBounds(30, 130, 193, 193);

        jLabel5.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jLabel5.setForeground(new java.awt.Color(213, 249, 213));
        jLabel5.setText("WELCOME TO COMPU SYSTEM DATABASE");
        getContentPane().add(jLabel5);
        jLabel5.setBounds(120, 10, 390, 40);

        jLabel6.setBackground(new java.awt.Color(255, 255, 255));
        jLabel6.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel6.setForeground(new java.awt.Color(255, 255, 255));
        jLabel6.setText("UNIVERSIDAD AUTONOMA DE GUERRERO");
        getContentPane().add(jLabel6);
        jLabel6.setBounds(190, 40, 240, 30);

        jLabel7.setBackground(new java.awt.Color(255, 255, 255));
        jLabel7.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel7.setForeground(new java.awt.Color(255, 255, 255));
        jLabel7.setText("UNIDAD ACADEMICA DE INGENIERIA");
        getContentPane().add(jLabel7);
        jLabel7.setBounds(200, 60, 202, 30);

        jLabel4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/welcome.jpg"))); // NOI18N
        getContentPane().add(jLabel4);
        jLabel4.setBounds(0, 0, 610, 340);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed

        
        String PASS = "";
        char[] p = jPasswordField1.getPassword();

        for (int i = 0; i < p.length; i++) {

            PASS = PASS + p[i];
        }

        System.out.println("p: " + PASS + "      log: " + jTextField1.getText());

        if (jTextField1.getText().length() > 0 && jPasswordField1.getPassword().length > 0) {
            conectar(jTextField1.getText(), PASS);
        } else {
            if (jTextField1.getText().length() == 0) {
                jLabel1.setForeground(Color.red);
                jLabel1.setFont(new Font("TimesRoman", Font.BOLD, 12));
                jLabel1.setText("Introduce nombre de usuario");

                if (jPasswordField1.getPassword().length == 0) {

                    jLabel2.setForeground(Color.red);
                    jLabel2.setFont(new Font("TimesRoman", Font.BOLD, 12));
                    jLabel2.setText("Introduce Contraseña");
                }
            } else {
                jLabel2.setForeground(Color.red);
                jLabel2.setFont(new Font("TimesRoman", Font.BOLD, 12));
                jLabel2.setText("Introduce contraseña");
            }
        }
        
        jTextField1.setText("");
        jPasswordField1.setText("");
        PASS ="";
        

    }//GEN-LAST:event_jButton1ActionPerformed

    public static void main(String args[]) {
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPasswordField jPasswordField1;
    private javax.swing.JTextField jTextField1;
    // End of variables declaration//GEN-END:variables
}
